<template>
  <el-form
      label-width="80px"
      style="max-width: 95%;">
    <el-form-item label="学生姓名">
      <el-select
          v-model="studentCourseQuery.stuId"
          placeholder="请选择学生姓名"
          style="width: 100%"
          clearable>
        <el-option
            v-for="item in studentOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"/>
      </el-select>
    </el-form-item>
    <el-form-item label="老师姓名">
      <el-select
          v-model="studentCourseQuery.teacherId"
          placeholder="请选择授课老师"
          style="width: 100%"
          clearable>
        <el-option
            v-for="item in teacherOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"/>
      </el-select>
    </el-form-item>

    <el-form-item label="课程名称">
      <el-select
          v-model="studentCourseQuery.courseId"
          placeholder="请选择课程名称"
          style="width: 100%"
          clearable>
        <el-option
            v-for="item in courseOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"/>
      </el-select>
    </el-form-item>

    <el-form-item>
      <el-input type="hidden" v-model="studentCourseQuery.id"/>
      <el-button type="primary" @click="addTeacherSubmit">提 交</el-button>
      <el-button type="success" plain @click="back">返 回</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {doGet, doPost} from "@/http/axios-http";
import {message} from "@/http/message";

export default {

  data() {
    return {
      //老师对象，初始值是空
      studentCourseQuery : {},
      //form表单错误对象，初始值是空
      formError : {},
      studentOptions:[{}],
      teacherOptions:[{}],
      courseOptions:[{}],
      teacherCourseOptions:[{}],
      teacherCourseRelation:{}
    }
  },

  mounted() {
    //根据路由id加载市场老师数据
    this.loadTeacherCourse();
    this.loadAllTeacher();
    this.loadAllCourse();
    this.loadAllStudent();
  },

  methods : {
    getCourseNameById(courseId,teacherId){
      //拿到课程名
      doGet('/course/course_info/getById/'+courseId).then(resp=>{
        this.teacherCourseRelation.courseName = resp.data.name
      })
      //拿到老师名
      doGet('/teacher/tea_info/getById/'+teacherId).then(resp=>{
        this.teacherCourseRelation.teacherName = resp.data.name
      })
      //分页查询一下
      doGet('/teacher_course/teacher_course_info/getTeacherCourseByPage',{
        teacherName:this.teacherCourseRelation.teacherName,
        courseName:this.teacherCourseRelation.courseName
      }).then(resp=>{
        this.teacherCourseOptions = resp.data.data
      })
      //把结果置换回来
      this.teacherCourseOptions.courseId = courseId
      this.teacherCourseOptions.teacherId = teacherId
    },
    loadAllStudent(){
      doGet('/student/stu_info/getAll').then(resp=>{
        console.log(resp.data)
        if(resp.data.code==200){
          this.studentOptions = resp.data.data;
        }else {
            //提示一下
            message("拿到学生信息失败，原因：" + resp.data.msg, "error");
          }
      })
    },
    loadAllTeacher(){
      doGet('/teacher/tea_info/getAll').then(resp=>{
        console.log(resp.data)
        if(resp.data.code==200){
          this.teacherOptions = resp.data.data;
        }else {
            //提示一下
            message("拿到老师信息失败，原因：" + resp.data.msg, "error");
          }
      })
    },
    loadAllCourse(){
      doGet('/course/course_info/getAll').then(resp=>{
        console.log(resp)
        if(resp.data.code==200){
          this.courseOptions = resp.data.data
        }else {
            //提示一下
            message("拿到课程信息失败，原因：" + resp.data.msg, "error");
          }
      })
    },
    //返回
    back() {
      //回退到上一次访问的页面
      //this.$router.go(-1);
      //精确地回到某个页面
      this.$router.push({path : "/teacherDash/student_course_info"})
    },

    //录入老师（提交保存）
    addTeacherSubmit() {
      this.studentCourseQuery.courseScore = 0
      //表单提交，使用formData提交表单
      let formData = new FormData();
      for (let field in this.studentCourseQuery) {
        formData.append(field, this.studentCourseQuery[field]);
      }
      if (this.studentCourseQuery.id) { //this.studentCourseQuery对象有id，说明的做了一次查询，那么就修改
        doPost("/student_course/student_course_info/save", formData).then(resp => {
          if (resp.data.code === 200) {
            //提示一下
            message("编辑学生选课信息成功", "success");
            //跳转到老师列表页
            this.$router.push({path : "/teacherDash/student_course_info"})
          } else {
            //提示一下
            message("编辑学生选课信息失败，原因：" + resp.data.msg, "error");
          }
        })
      } else {
        doPost("/student_course/student_course_info/save", formData).then(resp => {
          console.log(resp)
          if (resp.data.code === 200) {
            //提示一下
            message("录入学生选课信息成功", "success");
            //跳转到老师授课列表页
            this.$router.push({path : "/teacherDash/student_course_info"})
          } else {
            //提示一下
            message("录入学生选课信息失败，原因：" + resp.data.msg, "error");
          }
        })
      }
    },

    //根据路由id加载老师数据
    loadTeacherCourse() {
      let id = this.$route.params.id;
      console.log(id)
      if (id) {
        //编辑需要把数据查出来
        doGet("/student_course/student_course_info/get/" + id).then(resp => {
          this.studentCourseQuery = resp.data.data
        })
      }
    }
  }
}
</script>